<template>
  <!-- 上传图片组件 -->
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="httprequest"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>

  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'

// new一个cos实例
var cos = new COS({

  SecretId: 'AKIDT2XwlU7d2JUZWL4gnqndO4eAA3rY7e3L',
  SecretKey: 'HRVvGUaZS5d6SirsmbKzX0Oiw4771mIi'
})
export default {
  data() {
    return {
      imageUrl: ''

    }
  },
  methods: {
    httprequest(params) {
      cos.putObject({
        Bucket: 'article84-1315647521', /* 存储桶名称，必须字段 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，例如ap-beijing，必须字段 */
        Key: params.file.name, /* 储存到储存桶文件名（例如1.jpg，a/b/test.txt），必须字段 */
        Body: params.file, /* 必须，上传文件对象，就是上传的文件的数据对象，可以是input[type="file"]标签选择本地文件后得到的file对象 */
        // 进度发生变化时的回调
        // 严格模式下，this指向的是undefined
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
        }
        // 上传完毕的回调，会返回一个错误或数据
      }, (err, data) => {
        // console.log(8888888, data)
        this.imageUrl = 'http://' + data.Location
        console.log(88888888888, this.imageUrl)
        console.log(err || data)
      })
      console.log(45, params)
    }
  }
}
</script>
<style lang="scss" scoped>

.imagevis{
::v-deep .el-upload--picture-card{
    display: none;

}
}
::v-deep .el-progress-bar{
width: 200px;
}
::v-deep.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  ::v-deep.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  ::v-deep.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  ::v-deep.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
